﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLTN.Data;
using DLTN.Entities;

public partial class Admin_ChuyenMuc_CNChuyenMuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpChuyenMuc + H_AnhDaiDien.Value : "~/Images/no_image.jpg";
        if (!Page.IsPostBack)
        {
            BindCatalog.BindSubChuyenMuc(cboSubChuyenMuc);
            BindCatalog.BindLoaiChuyenMuc(cboLoaiChuyenMuc);
            if (Request.QueryString["id"] != null)
            {
                H_CatalogId.Value = Request.QueryString["id"].ToString();
                BindData(Convert.ToInt32(H_CatalogId.Value));
            }

            TList<SubChuyenMuc> TList_Catalogs = DataRepository.SubChuyenMucProvider.Find("IdLoaiChuyenMuc=" + cboLoaiChuyenMuc.SelectedValue);
            cboSubChuyenMuc.DataSource = TList_Catalogs;
            cboSubChuyenMuc.DataTextField = "TenSubChuyenMuc";
            cboSubChuyenMuc.DataValueField = "Id";
            cboSubChuyenMuc.DataBind();
        }
    }
    protected void BindData(int IdCategories)
    {
        using (ChuyenMuc entChuyenMuc = DataRepository.ChuyenMucProvider.GetById(IdCategories))
        {
            using (SubChuyenMuc ent_SubChuyenMuc = DataRepository.SubChuyenMucProvider.GetById(entChuyenMuc.IdSubChuyenMuc))
            {
                using (LoaiChuyenMuc ent_LoaiChuyenMuc = DataRepository.LoaiChuyenMucProvider.GetById(ent_SubChuyenMuc.IdLoaiChuyenMuc))
                {
                    H_CatalogId.Value = entChuyenMuc.Id.ToString();
                    cboLoaiChuyenMuc.SelectedValue = ent_LoaiChuyenMuc.Id.ToString();
                }
            }
            entChuyenMuc.LuotXem = Convert.ToInt32(H_LuotXem.Value);
            cboSubChuyenMuc.SelectedValue = entChuyenMuc.IdSubChuyenMuc.ToString();
            txtTieuDeTin.Text = entChuyenMuc.TieuDe;
            H_AnhDaiDien.Value = entChuyenMuc.AnhDaiDien;
            imgViewer.ImageUrl = entChuyenMuc.AnhDaiDien.Length > 0 ? Common.sPathUpChuyenMuc + entChuyenMuc.AnhDaiDien : "~/Images/no_image.jpg";
            txtTomTat.Text = entChuyenMuc.TomTat;
            txtContent.Text = Server.HtmlDecode(entChuyenMuc.NoiDung);
            txtNguon.Text = entChuyenMuc.Nguon;
            chkTrangThai.Checked = entChuyenMuc.TrangThai;
        }
    }

    protected void cmdLuu_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            ChuyenMuc entChuyenMuc = new ChuyenMuc();
            entChuyenMuc.Id = Convert.ToInt32(H_CatalogId.Value);
            entChuyenMuc.LuotXem = Convert.ToInt32(H_LuotXem.Value);
            entChuyenMuc.IdSubChuyenMuc = Convert.ToInt32(cboSubChuyenMuc.SelectedValue);
            entChuyenMuc.TieuDe = Common.StandardString(txtTieuDeTin.Text);
            entChuyenMuc.TomTat = Common.StandardString(txtTomTat.Text);
            entChuyenMuc.NoiDung = Server.HtmlEncode(txtContent.Text);
            entChuyenMuc.Nguon = Common.StandardString(txtNguon.Text);
            if (H_AnhDaiDien.Value.Length == 0)
            {
                entChuyenMuc.AnhDaiDien = "no_image.jpg";
            }
            else
            {
                entChuyenMuc.AnhDaiDien = Common.StandardString(H_AnhDaiDien.Value);
            }

            entChuyenMuc.NgayCapNhat = Common.GetServerDateTime();
            entChuyenMuc.TrangThai = chkTrangThai.Checked;

            if (entChuyenMuc.Id == 0)
            {
                entChuyenMuc.Id = 0;
                updateStatus = DataRepository.ChuyenMucProvider.Insert(entChuyenMuc);
            }
            else
            {
                entChuyenMuc.EntityState = EntityState.Changed;
                updateStatus = DataRepository.ChuyenMucProvider.Update(entChuyenMuc);
                //BindData(Convert.ToInt32(H_CatalogId.Value));
                //cmdNhapTiep_Click(sender,e);
            }

            if (!updateStatus)
                //WebMessage.Show(Constants.CHUYENMUC_CAPNHAT_THATBAI);
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.CHUYENMUC_CAPNHAT_THATBAI, true);
            else
            {
                //WebMessage.Show(Constants.CHUYENMUC_CAPNHAT_THANHCONG);
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.CHUYENMUC_CAPNHAT_THANHCONG, true);
                H_CatalogId.Value = entChuyenMuc.Id.ToString();
                Response.Redirect("~/Admin/ChuyenMuc/QLChuyenMuc.aspx");
            }

        }
        catch (Exception ex)
        {
            Common.WriteLog("CNChuyenMuc.aspx.cs", "cmdLuu_Click", ex.ToString());
            //WebMessage.Show(Constants.CHUYENMUC_CAPNHAT_THATBAI);
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.CHUYENMUC_CAPNHAT_THATBAI, true);
        }
    }

    protected void cmdNhapTiep_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "0";
        H_AnhDaiDien.Value = "~/Images/no_image.jpg";
        txtTieuDeTin.Text = "";
        txtTomTat.Text = "";
        chkTrangThai.Checked = true;
        txtContent.Text = "";
        txtNguon.Text = "";
        BindCatalog.BindLoaiChuyenMuc(cboLoaiChuyenMuc);
        BindCatalog.BindSubChuyenMuc(cboSubChuyenMuc);
    }
    protected void SelectedindexchangedcboLoaiChuyenMuc(object sender, EventArgs e)
    {
        TList<SubChuyenMuc> TList_Catalogs = DataRepository.SubChuyenMucProvider.Find("IdLoaiChuyenMuc=" + cboLoaiChuyenMuc.SelectedValue);
        cboSubChuyenMuc.DataSource = TList_Catalogs;
        cboSubChuyenMuc.DataTextField = "TenSubChuyenMuc";
        cboSubChuyenMuc.DataValueField = "Id";
        cboSubChuyenMuc.DataBind();
    }
}